<?php

namespace app\Admin\Controllers;

use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;

class AdminController extends Controller
{
    use HasResourceActions;

    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'Title';

    /**
     * Set description for following 4 action pages.
     *
     * @var array
     */
    protected $description = [
//        'index'  => 'Index',
//        'show'   => 'Show',
//        'edit'   => 'Edit',
//        'create' => 'Create',
    ];

    /**
     * Get content title.
     *
     * @return string
     */
    protected function title()
    {
        return $this->title;
    }

    public function index(Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['index'] ?? trans('admin.list'))
            ->body($this->grid());
    }

    public function show($id, Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['show'] ?? trans('admin.show'))
            ->body($this->detail($id));
    }

    public function edit($id, Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['edit'] ?? trans('admin.edit'))
            ->body($this->form()->edit($id));
    }

    public function create(Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['create'] ?? trans('admin.create'))
            ->body($this->form());
    }

    // 上传图片
    public function imageUpload(Request $request)
    {
        $file = $request->file('image');
        $validator = Validator::make(['image' => $file,], ['image' => 'image']);

        if ($validator->fails()) {
            return error(10, '请选择正确的图片');
        }

        $originalName = Str::random(6) . '_' . $file->getClientOriginalName();
        $filename = $this->imagePath() . '/' . $originalName;
        try {
            $disk = config('admin.upload.disk');
            $res = Storage::disk($disk)->put($filename, file_get_contents($file->getRealPath()));
            if ($res) {
                $url = config('filesystems.disks.' . $disk . '.domains.default') . $filename;
                return success('success', compact('url', 'originalName'));
            }
        } catch (\Exception $e) {
            return error(9, '上传图片失败，' . $e->getMessage());
        }

        return error(10, '上传图片失败，请检查网络！');
    }

    protected function imagePath() {
        return '/images/article/' . date('Y') . '/' . date('m') . '/'. date('d');
    }

    protected function sitePath() {
        return '/images/site/' . date('Y') . '/' . date('m') . '/'. date('d');
    }
}
